<template>
    <div>
        <div class="con1">
            <el-form :inline="true" class="demo-form-inline">
                <el-form-item label="处方号" >
                    <el-input v-model="idn"  placeholder="请输入处方号..."></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">查询</el-button>
                </el-form-item>
            </el-form>
            <el-divider></el-divider>
        </div>
        <div class="con2">
            <el-table
                    :data="tableData"
                    style="width: 100%">
                <el-table-column type="expand">
                    <template slot-scope="props">
                        <el-card>
                            <el-table class="flag"  row-class-name="row" header-row-class-name="row"
                                      :data="props.row.prescriptionDetaileds"
                                      style="width: 100%">
                                <el-table-column
                                        prop="drugs.drugsName"
                                        label="药品名称"
                                        width="180">
                                </el-table-column>
                                <el-table-column
                                        prop="drugs.drugsUnit"
                                        label="计量单位"
                                >
                                </el-table-column>
                                <el-table-column
                                        prop="amount"
                                        label="数量"
                                >
                                </el-table-column>
                                <el-table-column
                                        prop="frequency"
                                        label="用法用量"
                                >
                                </el-table-column>
                            </el-table>
                        </el-card>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="prescription.id"
                        label="处方号"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="prescription.prescriptionName"
                        label="项目内容"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="prescription.prescriptionTime"
                        label="开立时间"
                        width="180"
                        :formatter="formatter">
                </el-table-column>
                <el-table-column
                        prop="prescription.prescriptionState"
                        label="处方状态"
                >
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.prescription.prescriptionState == '1' ? 'danger' : 'primary'"
                                size="small" plain>
                            {{scope.row.prescription.prescriptionState==1?'暂存':scope.row.prescription.prescriptionState==2?'已开立':scope.row.prescription.prescriptionState==3?'已收费':'已作废'}}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        fixed="right"
                        label="操作">
                    <template slot-scope="scope">
                        <el-button @click="pay(scope.row)" type="info" size="small"
                                   :disabled="scope.row.prescription.prescriptionState !=2 ? true: false">交费
                        </el-button>
                        <el-button @click="payDel(scope.row)" type="info" size="small"
                                   :disabled="scope.row.prescription.prescriptionState !=3 ? true: false">退费
                        </el-button>
                    </template>
                </el-table-column>

            </el-table>
            <el-pagination
                    @current-change="handleCurrentChange"
                    :current-page.sync="currentPage1"
                    :page-size="pagesize1"
                    layout="total, prev, pager, next,jumper"
                    :total="totalNum">
            </el-pagination>
        </div>
    </div>
</template>

<script>
    import {HOST} from '../../config/HOST'
    export default {
        data() {
            return {
                idn:'',
                pagesize1: 5,
                totalNum: 0,
                currentPage1: 1,
                tableData: []
            }
        },
        created() {
            this.$ajax.get(`${HOST}prescriptions`).then((res) => {
                this.tableData = res.data.list
                this.totalNum = res.data.total
            })
        },
        methods: {
            today(){
                this.$ajax.get("http://localhost:8082/queryTodayCheck").then((res) => {
                    this.tableData = res.data
                })
            },
            handleCurrentChange(val) {
                this.$ajax.get(`${HOST}prescriptions?currentPage=${val}`).then((res) => {
                    this.tableData = res.data.list
                })
            },
            pay(row){
                row.prescription.prescriptionState=3
                console.log(row.prescription)
                this.$ajax.put(`${HOST}updatePrescriptionFee`,row.prescription).then((res) => {
                    if(res.data="success") this.$message("收费成功")
                    else this.$message("收费失败")
                })

            },
            payDel(row){
                row.prescription.prescriptionState=0
                this.$ajax.put(`${HOST}updatePrescriptionFee`,row.prescription).then((res) => {
                    if(res.data="success") this.$message("退费成功")
                    else this.$message("退费失败")
                })
            },
            onSubmit() {
                this.$ajax.get(`${HOST}getPrescription?id=${this.idn}`).then((res) => {
                    this.tableData=[]
                    this.tableData = res.data.list
                    this.totalNum = res.data.total
                })
            },
            formatter(row, column) {
                let d = new Date(row.prescription.prescriptionTime);
                let year = d.getFullYear();
                let month = d.getMonth();
                let day = d.getDate();
                let hour = d.getHours();
                let minute = d.getMinutes();
                let s = year + "-" + this.format(month + 1) + "-" + this.format(day) + " " + this.format(hour) + ":" + this.format(minute)
                return s
            },
            format(d) {
                return d > 9 ? d : '0' + d;
            },
        }
    }
</script>

<style>
    .con1 {
        text-align: center;
    }
    .row{
        color: #272a69;
    }
</style>